<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="driverIdcard">
        <el-input
          v-model="queryParams.driverIdcard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="driverContact">
        <el-input
          v-model="queryParams.driverContact"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驾驶证号" prop="drivingLicenseNo">
        <el-input
          v-model="queryParams.drivingLicenseNo"
          placeholder="请输入驾驶证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['user:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:info:edit']"
        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['user:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户编码" align="center" prop="userId" />
      <el-table-column label="用户姓名" align="center" prop="driverName" />
      <el-table-column label="身份证号" align="center" prop="driverIdcard" />
      <el-table-column label="身份证1" prop="photoCode"  align="center" width="150">
        <template #default="scope">
          <el-image :src="scope.row.photoCode" fit="cover" style="width: 100%; height: 200px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="身份证2" prop="photoCode"  align="center" width="150">
        <template #default="scope">
          <el-image :src="scope.row.res2" fit="cover" style="width: 100%; height: 200px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="driverContact" />
      <el-table-column label="驾驶证编码" align="center" prop="drivingLicenseNo" />
      <el-table-column label="准驾车型" align="center" prop="drivingType" />
      <el-table-column label="驾驶证有效期" align="center" prop="drivingLicenseExpiryDate" />
      <el-table-column label="驾驶证初领日期" align="center" prop="drivingLicenseFirstDate" />
      <el-table-column label="驾照照片" prop="photoCode"  align="center" width="150">
        <template #default="scope">
          <el-image :src="scope.row.drivingLicenseImg" fit="cover" style="width: 100%; height: 160px;"></el-image>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态" align="center" prop="driverStatus" />-->
      <el-table-column label="认证状态" align="center" prop="photoStatus" >
        <template #default="scope">
          <el-switch
              v-model="scope.row.photoStatus"
              active-value="0"
              inactive-value="1"
              @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="拒绝愿意" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['user:info:edit']">审核</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['user:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" v-model="open" width="680px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户姓名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="driverIdcard">
          <el-input v-model="form.driverIdcard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="照片">
          <el-image :src="form.photoCode" fit="cover" style="width: 45%; height: 320px;"></el-image>
          <el-image :src="form.res2" fit="cover" style="width: 45%; height: 320px;"></el-image>
        </el-form-item>
        <el-form-item label="联系方式" prop="driverContact">
          <el-input v-model="form.driverContact" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="驾驶证编码" prop="drivingLicenseNo">
          <el-input v-model="form.drivingLicenseNo" placeholder="请输入驾驶证编码" />
        </el-form-item>
        <el-form-item label="驾驶证图片" prop="drivingLicenseImg">
          <el-image :src="form.drivingLicenseImg" fit="cover" style="width: 45%; height: 320px;"></el-image>
        </el-form-item>
<!--        <el-form-item label="审核结果" prop="photoStatus">-->
<!--          <el-input v-model="form.photoStatus" type="" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
        <el-form-item label="审核结果">
          <el-radio-group v-model="form.photoStatus">
            <el-radio v-for="dict in sys_common_status" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="拒绝原因" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/user/info";

const { proxy } = getCurrentInstance();

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const { sys_common_status, sys_user_sex } = proxy.useDict("sys_common_status", "sys_user_sex");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    driverName: null,
    driverIdcard: null,
    photoCode: null,
    pinyin: null,
    driverContact: null,
    drivingLicenseNo: null,
    drivingType: null,
    drivingLicenseImg: null,
    drivingLicenseExpiryDate: null,
    drivingLicenseFirstDate: null,
    driverStatus: null,
    photoStatus: null,
    flag: null,
    res2: null,
    res3: null,
  },
  rules: {
    driverName: [
      { required: true, message: "用户姓名不能为空", trigger: "blur" }
    ],
    driverIdcard: [
      { required: true, message: "身份证号不能为空", trigger: "blur" }
    ],
    driverContact: [
      { required: true, message: "联系方式不能为空", trigger: "blur" }
    ],
    drivingLicenseNo: [
      { required: true, message: "驾驶证编码不能为空", trigger: "blur" }
    ],
    drivingLicenseExpiryDate: [
      { required: true, message: "驾驶证有效期不能为空", trigger: "blur" }
    ],
    driverStatus: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
/** 用户状态修改  */
function handleStatusChange(row) {
  let text = row.photoStatus === "0" ? "启用" : "拒绝";
  proxy.$modal.confirm('确认要"' + text + '""' + row.driverName + '"用户吗?').then(function () {
    return changeUserStatus(row.driverId, row.photoStatus);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status === "0" ? "1" : "0";
  });
};
// 表单重置
function reset() {
  form.value = {
    userId: null,
    driverName: null,
    driverIdcard: null,
    photoCode: null,
    pinyin: null,
    driverContact: null,
    drivingLicenseNo: null,
    drivingType: null,
    drivingLicenseImg: null,
    drivingLicenseExpiryDate: null,
    drivingLicenseFirstDate: null,
    driverStatus: null,
    photoStatus: null,
    flag: null,
    delFlag: null,
    res2: null,
    res3: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _userId = row.userId || ids.value
  getInfo(_userId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "审核用户信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.userId != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("审核已完成");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除用户信息编号为"' + _userIds + '"的数据项？').then(function() {
    return delInfo(_userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('user/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

getList();
</script>
